<?php

function upload_fun($up_info, $to_path, $typelist = array(), $file_size = 2000000)
{

    //1.判断文件上传是否错误

    if ($up_info['error'] > 0) {
        switch ($up_info['error']) {
            case 1:
                $err_info = "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
                break;
            case 2:
                $err_info = "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
                break;
            case 3:
                $err_info = "文件只有部分被上传";
                break;
            case 4:
                $err_info = "没有文件被上传";
                break;
            case 6:
                $err_info = "找不到临时文件夹";
                break;
            case 7:
                $err_info = "文件写入失败";
                break;
            default:
                $err_info = "未知的上传错误";
                break;
        }

        return $err_info;
        die();
    }

    //2.判断上传文件类型是否合法 1

    if (count($typelist) > 0) {
        if (!in_array($up_info['type'], $typelist)) {
            $err_info = '文件类型不合法！' . $up_info['type'];
            return $err_info;
            die();
        }
    }

    //4.判断上传文件大小是否超出允许值

    if ($up_info['size'] > $file_size) {
        return $err_info = '文件大小超过' . $file_size;
        die();
    }

    //5.上传文件重命名

    $exten_name = pathinfo($up_info['name'], PATHINFO_EXTENSION);

    do {
        $main_name = date('YmdHis' . '--' . rand(100, 999));
        $new_name  = $main_name . '.' . $exten_name;
    } while (file_exists($to_path . '/' . $new_name));

    //6.判断是否是上传的文件，并移动文件

    if (is_uploaded_file($up_info['tmp_name'])) {
        if (move_uploaded_file($up_info['tmp_name'], $to_path . '/' . $new_name)) {
            $err_info = $to_path . '/' . $new_name;
            return $err_info;
            die();
        } else {
            return $err_info = '上传文件移动失败！';
            die();
        }
    } else {
        return $err_info = '这个文件不是上传文件！';
        die();
    }
}

$typelist = array("image/gif", "image/jpeg", "image/pjpeg", "image/png");
//函数使用方法如下参考http://www.php.cn/php-weizijiaocheng-370392.html ##+++++++

$multi_info = $_FILES; //接收$_FILES的全部上传信息

$to_path = 'upload'; //上传文件的目标路径

foreach ($multi_info as $name => $up_info) {
    //foreach 遍历$multi_info

    $err_info = upload_fun($up_info, $to_path); //调用单文件上传函数

    print_r($err_info); //输出返回信息；
}
